package org.bees.cms.web.util.velocity.editer;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.bees.cms.web.util.Strings;

import net.sf.json.JSONObject;

public abstract class AbstractMultipleEditer extends AbstractEditer {
	// 数据来源 url 默认情况下 如果有 map 就不再访问url来取数据,输出的值也是一个 json格式 符合map接受的形式的json
	protected String url;
	// 间隔之间的内容
	protected String gap = "";
	// 以选中的所有值 形如:['value','value',...]
	protected List<String> values = new ArrayList<String>();
	// {title:value,title:value,...}
	protected Map<String, String> map = new HashMap<String, String>();

	@Override
	public void initialize(JSONObject json) throws Exception {
		super.initialize(json);
		url = json == null ? url : json.containsKey("url") ? json.getString("url") : url;
		gap = json == null ? gap : json.containsKey("gap") ? json.getString("gap") : gap;
		values = json == null ? values : json.containsKey("values") ? Arrays.asList(Strings.split("[", "]", ",", json.getString("values"))) : values;
		JSONObject object = json == null ? new JSONObject() : json.containsKey("map") ? json.getJSONObject("map") : new JSONObject();
		Iterator<?> iterator = object.keys();
		while (iterator.hasNext()) {
			String key = (String) iterator.next();
			String value = object.getString(key);
			map.put(key, value);
		}
	}

	@Override
	public abstract String edit(Serializable t) throws Exception;

	@Override
	public void destroy() throws Exception {
		super.destroy();
		url = null;
		gap = null;
		values = null;
		map = null;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public String getGap() {
		return gap;
	}

	public void setGap(String gap) {
		this.gap = gap;
	}

	public List<String> getValues() {
		return values;
	}

	public void setValues(List<String> values) {
		this.values = values;
	}

	public Map<String, String> getMap() {
		return map;
	}

	public void setMap(Map<String, String> map) {
		this.map = map;
	}

}
